package com.camemax.controller;

import com.camemax.pojo.UserBehavior;
import com.camemax.utils.UV_ProcessAllWindowFunction;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.triggers.Trigger;
import org.apache.flink.streaming.api.windowing.triggers.TriggerResult;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;

import java.net.URL;

public class FlinkWithBloomFilter {
    public static void main(String[] args) throws Exception {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment().setParallelism(1);

        URL resource = FlinkWithBloomFilter.class.getResource("/UserBehavior.csv");
        DataStream<UserBehavior> fileDataStream = env.readTextFile(resource.getPath())
                .map((MapFunction<String, UserBehavior>) line -> {
                    String[] fields = line.split(",");
                    return new UserBehavior(Long.valueOf(fields[0]), Long.valueOf(fields[1]), Integer.valueOf(fields[2]), fields[3], Long.valueOf(fields[4]));
                })
                .assignTimestampsAndWatermarks(WatermarkStrategy.<UserBehavior>noWatermarks().withTimestampAssigner((SerializableTimestampAssigner<UserBehavior>) (element, recordTimestamp) -> element.getTimestamp() * 1000L))
                .filter( value -> "pv".equals(value.getBehavior()));

        fileDataStream.filter(data -> "pv".equals(data.getBehavior()))
                .windowAll(TumblingEventTimeWindows.of(Time.hours(1)))
                // 2021-05-26 13:29:40
                // 配置Flink窗口计算触发器，其含有四种返回类型
                //  => TriggerRusult.CONTINUE —— 不作任何处理
                //  => TriggerResult.FIRE —— 执行计算操作，不清空窗口数据
                //  => TriggerResult.FIRE_AND_PURGE —— 执行计算操作并清空窗口数据
                //  => TriggerResult.PURGE —— 清空窗口数据
                .trigger(new Trigger<UserBehavior, TimeWindow>() {
                    @Override
                    public TriggerResult onElement(UserBehavior element, long timestamp, TimeWindow window, TriggerContext ctx) throws Exception {
                        // 每次数据输入，直接触发窗口计算，并清空窗口数据
                        return TriggerResult.FIRE_AND_PURGE;
                    }

                    @Override
                    public TriggerResult onProcessingTime(long time, TimeWindow window, TriggerContext ctx) throws Exception {
                        // 每次ProcessingTime窗口到期时，不操作
                        return TriggerResult.CONTINUE;
                    }

                    @Override
                    public TriggerResult onEventTime(long time, TimeWindow window, TriggerContext ctx) throws Exception {
                        // EventTime窗口到期时，不操作
                        return TriggerResult.CONTINUE;
                    }

                    @Override
                    public void clear(TimeWindow window, TriggerContext ctx) throws Exception {

                    }
                })
                .process(new UV_ProcessAllWindowFunction())
                .print();

        env.execute();
    }
}
